package me.firstapp.api.shop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.Sku;
import me.firstapp.service.shop.SkuService;

@Controller
@RequestMapping(value = "/sku")
@Api(value = "/sku", description = "商品属性相关接口")
public class SkuApi extends BaseApi {

	@Autowired
	private SkuService skuService;

	@RequestMapping(value = "findSkuPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询sku", notes = "分页查询sku")
	public void findSkuPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Sku> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Sku> skuPage = skuService.findSkuPage(pageNo, pageSize, searchParams, sortParam, sortType,
					apiClientId);
			JsonPage<Sku> jsonPage = new JsonPage<Sku>();
			jsonPage.setList(skuPage.getEntities());
			jsonPage.setPageNo(skuPage.getPageNo());
			jsonPage.setPageSize(skuPage.getPageSize());
			jsonPage.setTotalCount(skuPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addSku.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增sku", notes = "新增sku")
	public void addSku(Long productId, String name, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Sku> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Sku sku = skuService.addSku(productId, name, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(sku);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateSku.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改ku", notes = "修改sku")
	public void updateSku(Long skuId, String name, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Sku> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Sku sku = skuService.updateSku(skuId, name, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(sku);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "deleteSku.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除ku", notes = "删除sku")
	public void deleteSku(String skuIds, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			skuService.deleteSkus(skuIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
